﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using dokuzeylulworkshop.BaseUtility;
using System.Collections.Generic;
using DokuzEylul.Sinav.Entities;
using System.Data.Common;

namespace DokuzEylul.Sinav.YonetimPaneli
{
    public partial class Yetki : BasePage
    {

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                gvRoles.DataBind();
            }
        }
        public override int m_MenuID
        {
            get
            {
                return 10103;
            }
        }
        private IList<Role> CurrentRoleList
        {
            get
            {
                IList<Role> _result = Session["Current_Role_List"] as IList<Role>;
                if (_result == null)
                    Session["Current_Role_List"] = _result = m_dbmaster.UserInstance.GetAllRoles();
                return _result;
            }
        }
        private Role CurrentRole
        {
            get
            {
                Role _result = Session["Current_Role"] as Role;
                if (_result == null)
                    Session["Current_Role"] = _result = new Role();
                return _result;

            }
        }
        public bool IsSave
        {
            get
            {
                return (bool)ViewState["IsSave"];
            }
            set
            {
                ViewState["IsSave"] = value;
            }
        }
        protected void btnYeni_Click(object sender, EventArgs e)
        {
            ClearCurrentRole();
            pnlcontent.Enabled = true;
            
        }
        protected void btnKaydet_Click(object sender, EventArgs e)
        {
            Session["Current_Role"] = null;
            CurrentRole.RoleCode = txtRoleAdi.Text;
            CurrentRole.RoleCodeDescription = txtRoleDesc.Text;
            m_dbmaster.dbfactory.Save(CurrentRole);

            pnlcontent.Enabled = false;

           
            ClearCurrentRole();
            gvRoles.DataBind();
        }

        private void ClearCurrentRole()
        {
            txtRoleAdi.Text = "";
            txtRoleDesc.Text = "";
            Session["Current_Role"] = null;
        }
        protected void btnSil_Click(object sender, EventArgs e)
        {
            if (CurrentRole == null)
            {
                ltlinfo.Text = "Yer Hangi Bir Yetki Seçilmedi.";
                return;
            }

            //kullanicilarin Rolleri temizleniyor
            //TODO: Transaction koyarsak iyi olur.
            DbCommand dbc = m_dbmaster.dbfactory.GetSqlStringCommand("Delete From SinavUserRole where RoleCode=@RoleCode");
            m_dbmaster.dbfactory.AddInParameter(dbc, "@RoleCode", DbType.String, CurrentRole.RoleCode);
            m_dbmaster.dbfactory.ExecuteNonQuery(dbc);

            m_dbmaster.dbfactory.DeleteDbEntity(CurrentRole);

            ClearCurrentRole();
            gvRoles.DataBind();

        }
        protected void gvRoles_DataBinding(object sender, EventArgs e)
        {
            Session["Current_Role_List"] = null;
            gvRoles.DataSource = CurrentRoleList;
        }

        protected void gvRoles_SelectedIndexChanged(object sender, EventArgs e)
        {
            pnlcontent.Enabled = true;
            Session["Current_Role"] = m_dbmaster.dbfactory.LoadEntityByID<Role>(gvRoles.SelectedValue);
            txtRoleAdi.Text = CurrentRole.RoleCode;
            txtRoleDesc.Text = CurrentRole.RoleCodeDescription;
        }
    }
}
